;(function(){

    new Vue({
      el:'#edit-contract-update',
      data:{
        alterId:hlUrlUtil.GetQueryString('id'),
        baseMes:{
          'alter_id':null,
          'alter_no':null,
          'alter_type':'YB',
          'alert_description':null,
          'm_code':null,
          'c_code':null,
          'name':null,
          'memo':null
        },
        zlfInfo:{
          'zlf_type':null,
          'zlf_id':null,
          'zlf_name':null,
          'idcode':null,
          'email':null,
          'zlf_lperson':null,
          'zlf_lphone':null,
          'zlf_sperson':null,
          'zlf_sphone':null,
          'zlf_address':null,
          'zlf_industry_type':null
        },
        zlfType: {
          list: [{name: '公司租赁', type: 'C'}, {name: '个人租赁', type: 'P'}],
          active: 'C'
        },
        industryTypes: {
          list: [{code: '',name:''}],
          selectedName: null,
          selectedCode: null
        },
        permissions: window.localStorage.getItem('permissions'),
        signingDate:'',
      },
      filters: {
        formatDate: function(time) {
          if (time === undefined)return '--'
          return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
        },
        formatUndefined: function(item) {
          if (!item)return '--'
          return item
        },
        formatNum: function(item) {
          return hlNumberUtil.formatNum(item)
        }
      },
      methods:{
        selectedZlf: function (type) {
          this.zlfType.active = type;
          this.zlfInfo.zlf_type = type;
          this.zlfInfo.idcode = null;
          this.zlfInfo.email = null;
        },
        //获取行业列表
        getIndustryTypes: function () {
          var self = this;
          hlhttp.get({
            url: 'configure/industry/dict',
            success: function (data) {
              self.industryTypes.list = data;
              self.getData()
            }
          })
        },
        //设置行业类型
        selectedIndustryType: function (type, name) {
          this.industryTypes.selectedCode = type;
          this.industryTypes.selectedName = name;
          this.zlfInfo.zlf_industry_type = type;
        },
        dataDeal:function(){
          var self = this;
          var data = {
            'edit_flag':'true',
            'alter_id':self.baseMes.alter_id,
            'alter_no':self.baseMes.alter_no,
            'm_code':self.baseMes.m_code,
            'alter_type':self.baseMes.alter_type,
            'alert_description':self.baseMes.alert_description ? self.baseMes.alert_description : '#',
            'effective_date':hlDateUtil.formatDate(new Date(),'yyyy-MM-dd'),
            'c_code':self.baseMes.c_code,
            'name':self.baseMes.name,
            'sign_time':this.signingDate,
            'memo':self.baseMes.memo ? self.baseMes.memo : '#',
            'zlf_info':JSON.stringify(self.zlfInfo)
          }
          return data;
        },
        //保存合同变更  变更合同变为草稿状态
        saveData:function(){
          var self = this;
          var valid = self.validate();
          var data = self.dataDeal();
          if(valid){
            hlhttp.post({
              url:'contract_alter',
              data:data,
              success:function(data){
                hl.toast('保存成功');
                setTimeout(function(){
                  hlUrlUtil.goTo('contractUpdateList',{ddtab:true},true)
                },2000)
              }
            })
          }
        },
        // 提交合同变更  变更合同变为生效状态
        submitData:function(){
          var self = this;
          var valid = self.validate();
          var data = self.dataDeal();

          if(valid){
            hlhttp.put({
              url:'contract_alter/_submit',
              data:data,
              success:function(data){
                hl.toast('提交成功');
                setTimeout(function(){
                  hlUrlUtil.goTo('contractUpdateList',{ddtab:true},true)
                },2000)
              }
            })
          }

        },
        // 同步招商数据
        syncData:function(){
          var self = this;

          hlhttp.get({
            url:'customer,records',
            data:{ 'customer_id':self.zlfInfo.zlf_id },
            success:function(data){
              var newObj = {
                'zlf_type':data.type == 'QY' ? 'C' : 'P',
                'zlf_name':data.name,
                'idcode':data.idcode,
                'email':data.email,
                'zlf_lperson':data.lperson,
                'zlf_lphone':data.phone,
                'zlf_industry_type':data.industry
              }
              self.zlfType.active = newObj.zlf_type;
              self.industryTypes.selectedCode = data.industry;

              self.industryTypes.selectedName = self.matchIndustryName(self.industryTypes.selectedCode)
              $('#industry-st .select-name').text(self.industryTypes.selectedName)
              Object.assign(self.zlfInfo,newObj)
            }
          })
        },
        validate:function(){
          if (!this.zlfInfo.zlf_name) {
            hl.toast('请填写客户名称')
            return false;
          }
          if (!this.industryTypes.selectedCode) {
            hl.toast('请选择所属行业')
            return false;
          }
          if (!this.zlfInfo.idcode) {
            if(this.zlfType.active === 'C'){
              hl.toast('请填写纳税人识别号')
              return false;
            }
            if(this.zlfType.active === 'P'){
              hl.toast('请填写身份证号')
              return false;
            }
          }
          if (!this.zlfInfo.zlf_sperson) {
            hl.toast('请填写联系人')
            return false;
          }
          if (!this.zlfInfo.zlf_sphone) {
            hl.toast('请填写联系电话')
            return false;
          }
          if (!this.zlfInfo.zlf_address) {
            hl.toast('请填写通讯地址')
            return false;
          }
          return true;
        },
        getData:function(){
          var self = this;
          hlhttp.get({
            url:'contract_alter',
            data:{'alter_id':self.alterId},
            success:function(data){
              var baseMes = {
                'alter_id':data.alterId,
                'alter_no':data.alterNo,
                'alter_type':'YB',
                'alert_description':data.alertDescription,
                'm_code':data.mCode,
                'c_code':data.cCode,
                'name':data.name,
                'memo':data.memo
              }
              var zlfInfo = {
                'zlf_type':data.zlfInfo.zlfType,
                'zlf_id':data.zlfInfo.zlfId,
                'zlf_name':data.zlfInfo.zlfName,
                'idcode':data.zlfInfo.idcode,
                'email':data.zlfInfo.email,
                'zlf_lperson':data.zlfInfo.zlfLperson,
                'zlf_lphone':data.zlfInfo.zlfLphone,
                'zlf_sperson':data.zlfInfo.zlfSperson,
                'zlf_sphone':data.zlfInfo.zlfSphone,
                'zlf_address':data.zlfInfo.zlfAddress,
                'zlf_industry_type':data.zlfInfo.zlfIndustryType
              }
              self.baseMes = baseMes;
              self.zlfInfo = zlfInfo;

              self.zlfType.active = data.zlfInfo.zlfType;
              self.industryTypes.selectedCode = data.zlfInfo.zlfIndustryType;
              self.industryTypes.selectedName = self.matchIndustryName(self.industryTypes.selectedCode)

              $('#signTime').attr('value',hlDateUtil.formatDate(data.signTime,'yyyy-MM-dd'))
              self.signingDate = hlDateUtil.formatDate(data.signTime,'yyyy-MM-dd')
              loginPrem.loginList()
            }
          })
        },
        matchIndustryName:function(code){
          var res = null;
          this.industryTypes.list.forEach((el,i) => {
            if(el.dictCode == code) res = el.dictName;
          })
          return res;
        }
      },
      mounted:function(){
        hlSelectUi()
        this.getIndustryTypes()
      }

    })
}())
